package com.amoby.main.action.cbabill;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amoby.base.common.BaseAction;
import com.amoby.base.common.Pager;
import com.amoby.base.utils.DateUtil;
import com.amoby.main.entity.SysUsers;
import com.amoby.main.entity.Verifier;
import com.amoby.main.service.CbaMgrService;
import com.amoby.main.service.UserService;
/**
 * @date 2015-7-6 下午3:12:31
 * @author zhaoyy
 * @version V1.0
 * @Description 授权人管理
 */
@Controller
public class VerifierAction extends BaseAction {
    @Autowired
    private UserService      userSv;
    @Autowired
    private CbaMgrService cbaMgrService;

    /**
     * 初始化页面
     * @author Lasing 2016-5-26 下午4:41:42
     * @param model
     * @return
     */
    @RequestMapping(value = "/menus/verifier_mgr.html", method = RequestMethod.GET)
    public String index(ModelMap model) {
        // 查询出所有的审核员
        List<SysUsers> usernames = userSv.selectAllVer();
        model.put("usernames", usernames);
        return AD_HTML + "cbabill/verifier_mgr";
    }

    /**
     * 授权人列表
     * @author Lasing 2016-5-26 下午4:42:06
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/verifier/list.html", method = RequestMethod.POST)
    public Object list(HttpServletRequest request) {
        Pager pager = createPager(request);
        pager.addParam("username", getRequestParams(String.class, request, "username"));
        pager.addParam("status", getRequestParams(String.class, request, "status"));
        pager.addParam("phone", getRequestParams(Integer.class, request, "phone"));
        cbaMgrService.getAllVerifier(pager);
        return getGridData(pager);
    }

    /**
     * 授权人姓名加载出电话号码,userId
     * @param request
     * @param model
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/verifier/changeName.html", method = RequestMethod.POST)
    public Object changeName(HttpServletRequest request, ModelMap model, String username,Long id) {
        Map<String, Object> resultMap = getResultMap();
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("username", username);
            map.put("id", id);
            // 查询是否有重复数据
            if (cbaMgrService.checkName(map) > 0) {
                resultMap.put(RESULT, false);
                resultMap.put(MESSAGE, "该审核人信息已经存在！");
            } else {
                // 授权人姓名加载出电话号码,userId
                map.clear();
                map.put("username", username);
                List<SysUsers> allNames = userSv.allNameList(map);
                resultMap.put("allNames", allNames);
                resultMap.put(RESULT, true);
                resultMap.put(MESSAGE, "操作成功");
            }
        } catch (Exception e) {
            getLog(this).error(e.getMessage(), e);
            resultMap.put(RESULT, false);
            resultMap.put(MESSAGE, "加载电话号码失败");
        }
        return resultMap;
    }

    /**
     * 新增信息
     * @param verifier
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/verifier/add.html", method = RequestMethod.POST)
    public Object add(Verifier verifier, HttpServletRequest request) {
        Map<String, Object> result = getResultMap();
        try {
            verifier.setCreateTime(DateUtil.getCurDate());
            verifier.setUpdateTime(DateUtil.getCurDate());
            Long userId = getRequestParams(Long.class, request, "userId");
            verifier.setUserId(userId);
            // 选择启用状态时，将数据库其他状态全部变停用，保证数据库只有一个启动状态
            if (verifier.getStatus() == 1) {
                cbaMgrService.update();
            }
            if (verifier.getUserId() == null) {
                result.put(RESULT, false);
                result.put(MESSAGE, "提交信息不完整！");
            } else if (cbaMgrService.addVerifier(verifier) > 0) {
                result.put(RESULT, true);
                result.put(MESSAGE, "新增成功！");
            } else {
                result.put(RESULT, false);
                result.put(MESSAGE, "新增失败！");
            }
        } catch (Exception e) {
            result.put(RESULT, false);
            result.put(MESSAGE, "系统异常，操作失败！");
            getLog(this).error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * 修改信息
     * @param verifier
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/verifier/upd.html", method = RequestMethod.POST)
    public Object upd(Verifier verifier, HttpServletRequest request) {
        Map<String, Object> result = getResultMap();
        verifier.setUpdateTime(DateUtil.getCurDate());
        Long userId = getRequestParams(Long.class, request, "userId");
        verifier.setUserId(userId);
        // 选择启用状态时，将数据库其他状态全部变停用，保证数据库只有一个启动状态
        if (verifier.getStatus() == 1) {
            cbaMgrService.update();
        }
        try {
            if (verifier.getUserId() == null) {
                result.put(RESULT, false);
                result.put(MESSAGE, "提交数据不完整！");
            } else if (cbaMgrService.updVerifier(verifier) > 0) {
                result.put(RESULT, true);
                result.put(MESSAGE, "修改成功！");
            } else {
                result.put(RESULT, false);
                result.put(MESSAGE, "修改失败！");
            }
        } catch (Exception e) {
            result.put(RESULT, false);
            result.put(MESSAGE, "系统异常，操作失败！");
            getLog(this).error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * 删除信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/verifier/del.html", method = RequestMethod.POST)
    public Object del(HttpServletRequest request, Long id) {
        Map<String, Object> result = getResultMap();
        try {
            if (id != null && cbaMgrService.delVerifier(id) > 0) {
                result.put(RESULT, true);
                result.put(MESSAGE, "删除审核员信息成功！");
            } else {
                result.put(MESSAGE, "删除审核员信息失败！");
            }
        } catch (Exception e) {
            result.put(MESSAGE, "删除审核员信息异常！");
            getLog(this).error("删除审核员信息异常！", e);
        }
        return result;
    }
}
